<template>
  <div id="tabledialog">
    <el-dialog :title="dialog.title" :visible.sync="dialog.show" width="30%" center class="dialog">
      <el-form ref="formData" label-width="60px" :model="dialogFormData" :rules="formRules">
        <el-form-item label="内容 :" prop="content">
          <el-input v-model="dialogFormData.content"></el-input>
        </el-form-item>

        <el-form-item label="收入 :" prop="income">
          <el-input v-model="dialogFormData.income"></el-input>
        </el-form-item>

        <el-form-item label="支出 :" prop="expend">
          <el-input v-model="dialogFormData.expend"></el-input>
        </el-form-item>

        <el-form-item label="备注 :">
          <el-input v-model="dialogFormData.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="dialog.show = false">取消</el-button>
        <el-button type="primary" @click="submitFormData('formData')">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formRules: {
        content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
        income: [{ required: true, message: "收入不能为空", trigger: "blur" }],
        expend: [{ required: true, message: "支出不能为空", trigger: "blur" }],
      },
    };
  },
  props: {
    dialog: {
      type: Object,
    },
    dialogFormData: {
      type: Object,
    },
  },
  methods: {
    submitFormData(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialog.option === "add") {
            this.$http.post("/data/add", this.dialogFormData).then((res) => {
              this.dialog.show = false;
              this.$message({
                type: "success",
                message: "添加成功",
              });
              this.$emit("updateData");
            });
          } else {
            this.$http.put(`/data/update/${this.dialog.id}`,this.dialogFormData)
            .then(res=>{
              this.dialog.show = false;
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.$emit("updateData");
            })
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
#tabledialog {
  .el-form {
    width: 320px;
    margin: 0 auto;
  }
}
</style>